﻿using CRM.Models;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using CRM.ViewModels.System;
using Microsoft.EntityFrameworkCore;


namespace CRM.Controllers.SystemManage
{
    [Route("api/[controller]")]
    [ApiController]
    public class CompanyPointsConfig : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly UserManager<User> _userManager;

        public CompanyPointsConfig(MyDbContext dbContext, UserManager<User> userManager)
        {
            _dbContext = dbContext;
            _userManager = userManager;
        }
        
        /// <summary>
        /// 获取积分通用配置(Id为1)
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<ResultStruct> GetPointsGeneralConfiguration()
        {
            var result = await _dbContext.CompanyPointsConfig.FirstAsync(m => m.Id == 1);
            return Success(result);
        }

        [HttpPost("[action]")]
        public async Task<ResultStruct> UpdatePointsGeneralConfiguration(CompanyPointsConfigVM vm)
        {
            //TODO: 限定特定人员才能调用
            var model = 
                await _dbContext.CompanyPointsConfig
                    .Where(m => m.Id == 1)
                    .FirstAsync();
            vm.UpdateModel(model);
            await _dbContext.SaveChangesAsync();
            return Success();
        }
        
        
    }
}